package top.oylan.mp.utils;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;


/**
 * Created by Administrator on 2018/12/23.
 */
@Component
public class FileUploader {

    @Value("${aylan.constant.upload-root-path}")
    private String rootPath;

    public String getRootPath() {
        return rootPath;
    }

    public Result upload(MultipartFile file, Directory dir) {

        StringBuffer sb = new StringBuffer();
        Calendar calendar = Calendar.getInstance();
        String year = String.valueOf(calendar.get(Calendar.YEAR));
        String month = String.valueOf(calendar.get(Calendar.MONTH) + 1);

        switch (dir) {
            case PRODUCT:
                sb.append("art/");
                sb.append(year);
                sb.append("/");
                sb.append(month);
                sb.append("/");
                break;
            case IMAGE:
                sb.append("image/");
                sb.append(year);
                sb.append("/");
                sb.append(month);
                sb.append("/");
                sb.append(String.valueOf(calendar.get(Calendar.DAY_OF_MONTH)));
                sb.append("/");
                break;
            case SYSTEM:
                sb.append("system/");
                sb.append(year);
                sb.append("/");
                break;
            case CATEGORY:
                sb.append("category/");
                sb.append(year);
                sb.append("/");
                break;
            default:
                sb.append("system/");
                sb.append(year);
                sb.append("/");
                sb.append(month);
                sb.append("/");
                break;
        }

        return doUpload(file, sb.toString());
    }

    public Result upload(MultipartFile file, String path) {

        return doUpload(file, path);
    }

    public Result doUpload(MultipartFile file, String targetFilePath) {

        Result result = new Result();
        if (file != null && !file.isEmpty()) {
            String fileName = file.getOriginalFilename();

            String type = fileName.substring(fileName.lastIndexOf("."), fileName.length());
            long time = System.currentTimeMillis();
            String saveFile = targetFilePath + String.valueOf(time) + type;
            File targetFile = new File(rootPath + saveFile);

            File directory = new File(rootPath + targetFilePath);

            if (!directory.isDirectory()) {
                directory.mkdirs();
            }

            try {
                file.transferTo(targetFile);
                result.setCode((byte) 1);
                result.setMsg("上传成功");
                Map<String, Object> map = new HashMap<String, Object>();
                map.put("path", "/" + saveFile);
                result.setData(map);
                targetFile.setReadable(true);
                targetFile.setWritable(true);
            } catch (IOException e) {
                e.printStackTrace();
                result.setCode((byte) -1);
                result.setMsg(e.getMessage());
            }
        } else {
            result.setCode((byte) 2);
            result.setMsg("请上传图片");
        }
        return result;
    }

    public void removeFile(String path) {
        if (!StringUtils.isEmpty(path)) {
            File file = new File(rootPath + path);
            if (file.exists()) {
                file.delete();
            }
        }
    }
}
